home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / QuickDraw3D 1.6 SDK / Mac SampleCode New for 1.6 / CompressedPixmapSample / Source / Process.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-18  |  13.3 KB  |  569 lines  |  [TEXT/CWIE]

  1. /****************************/
  2. /*       PROCESS.C             */
  3. /* By Brian Greenstone      */
  4. /****************************/
  5.  
  6.  
  7. /****************************/
  8. /*    EXTERNALS             */
  9. /****************************/
  10. #include <Resources.h>
  11. #include <NumberFormatting.h>
  12.  
  13. #include <QuickTimeComponents.h>
  14. #include <QD3D.h>
  15. #include <QD3DGeometry.h>
  16. #include <QD3DMath.h>
  17. #include <QD3DView.h>
  18. #include <QD3DGroup.h>
  19. #include <QD3DTransform.h>
  20. #include <QD3DStorage.h>
  21.  
  22. #include "myglobals.h"
  23. #include "misc.h"
  24. #include "process.h"
  25. #include "qd3d_support.h"
  26.  
  27. extern    float                gFramesPerSecond;
  28.  
  29.  
  30. /****************************/
  31. /*    PROTOTYPES            */
  32. /****************************/
  33.  
  34. static void InitModelWindow(void);
  35. static void BuildTestModel(void);
  36. static    TQ3SurfaceShaderObject    QD3D_GetTextureMap(long    textureRezID);
  37. static    TQ3SurfaceShaderObject    QD3D_PICTToTexture(PicHandle picture);
  38.  
  39.  
  40. /****************************/
  41. /*    CONSTANTS             */
  42. /****************************/
  43.  
  44. #define    MODEL_WIND_ID            400
  45.  
  46. #define    TEXTURE_ORIGINAL_DEPTH    32
  47.  
  48. /*********************/
  49. /*    VARIABLES      */
  50. /*********************/
  51.  
  52. WindowPtr                gModelWindow = nil;
  53. QD3DSetupOutputType        gModelViewInfo;
  54. TQ3Object                gModelGroup = nil;
  55. TQ3GroupObject            gBaseGroup = nil;
  56.  
  57. CodecQ            gCodecQuality     = codecNormalQuality;
  58. CodecType        gCodecType        = kRawCodecType;
  59. CodecComponent     gCodecComponent = anyCodec;
  60. short            gCodecDepth        = TEXTURE_ORIGINAL_DEPTH;
  61. short            gCodecDepth2    = TEXTURE_ORIGINAL_DEPTH;
  62.  
  63. TQ3AttributeSet        gTextureAttr;
  64.  
  65. TQ3Matrix4x4    gTransformMatrix;
  66. TQ3Object        gTransformObject = nil;
  67.  
  68. Str255            gCompressionRatio,gCompressionSize;
  69.  
  70.  
  71.  
  72. #pragma mark ========INITIALIZATION STUFF=========
  73.  
  74. /******************** INIT TEST ************************/
  75.  
  76. void InitTest(void)
  77. {
  78.  
  79.             /* INIT THE WINDOW */
  80.             
  81.     InitModelWindow();
  82.  
  83.  
  84.             /* CREATE THE TEST MODEL */
  85.             
  86.     BuildTestModel();
  87.  
  88.  
  89.             /* CREATE DISPLAY GROUP */
  90.             
  91.     gBaseGroup = Q3DisplayGroup_New();                                        // make display group     
  92.     Q3Matrix4x4_SetIdentity(&gTransformMatrix);
  93.     gTransformObject = Q3MatrixTransform_New(&gTransformMatrix);            // make transform object
  94.     
  95.     Q3Group_AddObject(gBaseGroup, gTransformObject);                        // add to group        
  96.     Q3Group_AddObject(gBaseGroup, gModelGroup);                                // add geometry to group 
  97.  
  98.  
  99.             /* DRAW THE WINDOW */
  100.                         
  101.     DrawModelWindow();
  102. }
  103.  
  104.  
  105.  
  106. /************** INIT MODEL WINDOW *******************/
  107.  
  108. static void InitModelWindow(void)
  109. {
  110. QD3DSetupInputType    viewDef;
  111. TQ3Point3D            cameraFrom = { 0,0, 100 };
  112. TQ3Point3D            cameraTo = { 0, 0, 0 };
  113. TQ3Vector3D            cameraUp = { 0.0, 1.0, 0 };
  114. TQ3ColorARGB        clearColor = {1,0,0,.5};
  115. TQ3ColorRGB            ambientColor = { 1.0, 1.0, 1.0 };
  116. TQ3Vector3D            fillDirection1 = {1, -.3, -.8 };
  117. TQ3Vector3D            fillDirection2 = { -1, -.1, -.2 };
  118.  
  119.  
  120.             /* CREATE THE WINDOW */
  121.             
  122.     gModelWindow = GetNewCWindow(MODEL_WIND_ID, nil,MOVE_TO_FRONT);
  123.     if (gModelWindow == nil)
  124.         DoFatalAlert("\pWhere did the GameWindow window go?");                    
  125.     SetPort((GrafPtr)gModelWindow);
  126.  
  127.  
  128.             /***********************/
  129.             /* SET QD3D PARAMETERS */
  130.             /***********************/
  131.  
  132.     viewDef.view.displayWindow         = gModelWindow;
  133.     viewDef.view.rendererType         = kQ3RendererTypeInteractive;
  134.     viewDef.view.clearColor         = clearColor;
  135.     viewDef.view.paneClip.left         = 0;
  136.     viewDef.view.paneClip.right     = 0;
  137.     viewDef.view.paneClip.top         = 16;
  138.     viewDef.view.paneClip.bottom     = 0; 
  139.  
  140.     viewDef.styles.interpolation     = kQ3InterpolationStyleVertex; 
  141.     viewDef.styles.backfacing         = kQ3BackfacingStyleBoth;
  142.     viewDef.styles.fill                = kQ3FillStyleFilled; 
  143.     viewDef.styles.illuminationType    = kQ3IlluminationTypePhong; 
  144.  
  145.     viewDef.camera.from             = cameraFrom;
  146.     viewDef.camera.to                 = cameraTo;
  147.     viewDef.camera.up                 = cameraUp;
  148.     viewDef.camera.hither             = 5;
  149.     viewDef.camera.yon                 = 3000;
  150.     viewDef.camera.fov                 = 1.0;
  151.  
  152.     viewDef.lights.ambientBrightness = 1.0;
  153.     viewDef.lights.ambientColor     = ambientColor;
  154.     viewDef.lights.numFillLights     = 2;
  155.     viewDef.lights.fillDirection[0] = fillDirection1;
  156.     viewDef.lights.fillDirection[1] = fillDirection2;
  157.     viewDef.lights.fillColor[0]     = ambientColor;
  158.     viewDef.lights.fillColor[1]     = ambientColor;
  159.     viewDef.lights.fillBrightness[0] = 1.1;
  160.     viewDef.lights.fillBrightness[1] = 0.4;
  161.  
  162.     QD3D_SetupWindow(&viewDef, &gModelViewInfo);
  163. }
  164.  
  165.  
  166.  
  167. /********************* BUILD TEST MODEL **************************/
  168.  
  169. static void BuildTestModel(void)
  170. {
  171. TQ3TriMeshData                    myTriMeshData;
  172. TQ3TriMeshAttributeData            vertexAttribs[3],faceAttribs;
  173.  
  174. TQ3Point3D points[3] = 
  175. {
  176.     0,35,0,
  177.     -35,-35,0,
  178.     35,-35,0
  179. };
  180.  
  181. TQ3Vector3D    vertexNormals[3] = 
  182. {
  183.     0,0,1,
  184.     0,0,1,
  185.     0,0,1
  186. };
  187.  
  188. TQ3Vector3D    faceNormals[1] =
  189. {
  190.     0,0,1
  191. };
  192.  
  193. TQ3Param2D uvArray[3] =
  194. {
  195.     .5,1,
  196.     0,0,
  197.     1,0
  198. };
  199.  
  200.  
  201. TQ3TriMeshTriangleData    triangles[1] =
  202. {
  203.     0,1,2
  204. };
  205.         
  206.  
  207. TQ3SurfaceShaderObject    texture;
  208.  
  209.  
  210.             /* FIRST LOAD THE TEXTUREMAP */
  211.                     
  212.     texture = QD3D_GetTextureMap(128);
  213.         
  214.     gTextureAttr = Q3AttributeSet_New();
  215.     Q3AttributeSet_Add(gTextureAttr, kQ3AttributeTypeSurfaceShader, &texture);        
  216.  
  217.             /* BUILD MAIN TRIMESH DATA STRUCTURE */
  218.  
  219.     myTriMeshData.triMeshAttributeSet = gTextureAttr;
  220.  
  221.     myTriMeshData.numTriangles = 1;
  222.     myTriMeshData.triangles = &triangles[0];
  223.  
  224.     myTriMeshData.numTriangleAttributeTypes = 1;
  225.     myTriMeshData.triangleAttributeTypes = &faceAttribs;
  226.  
  227.     myTriMeshData.numEdges = 0;
  228.     myTriMeshData.edges = nil;
  229.     myTriMeshData.numEdgeAttributeTypes = 0;
  230.     myTriMeshData.edgeAttributeTypes = nil;
  231.  
  232.     myTriMeshData.numPoints = 3;
  233.     myTriMeshData.points = &points[0];
  234.  
  235.     myTriMeshData.numVertexAttributeTypes = 1;
  236.      myTriMeshData.vertexAttributeTypes = &vertexAttribs[0];
  237.  
  238.  
  239.             /* CALCULATE BOUNDING BOX */
  240.  
  241.     Q3BoundingBox_SetFromPoints3D(&myTriMeshData.bBox, &points[0],    3, sizeof(TQ3Point3D));
  242.  
  243.  
  244.             /* CREATE FACE ATTRIBUTES */ 
  245.  
  246.     faceAttribs.attributeType = kQ3AttributeTypeNormal;
  247.     faceAttribs.data = &faceNormals[0];
  248.     faceAttribs.attributeUseArray = nil;
  249.  
  250.  
  251.             /* CREATE VERTEX ATTRIBUTES */ 
  252.  
  253.     vertexAttribs[0].attributeType = kQ3AttributeTypeSurfaceUV;
  254.     vertexAttribs[0].data = &uvArray[0];
  255.     vertexAttribs[0].attributeUseArray = nil;
  256.  
  257.  
  258.         /* MAKE THE TRIMESH GEOMETRY OBJECT */
  259.  
  260.     gModelGroup = Q3TriMesh_New(&myTriMeshData);
  261.     if (gModelGroup == nil)
  262.         DoFatalAlert("\pQ3TriMesh_New failed!");
  263.  
  264.  
  265. }
  266.  
  267.  
  268. #pragma mark =========UPDATING============
  269.  
  270.  
  271. /*************** DO MODEL WINDOW NULL EVENT **********************/
  272.  
  273. void DoModelWindowNullEvent(void)
  274. {
  275. TQ3Matrix4x4    m;
  276.  
  277.             /* ROTATE THE GEOMETRY */
  278.             
  279.     QD3D_CalcFramesPerSecond();
  280.     Q3Matrix4x4_SetRotate_XYZ(&m,0.4/gFramesPerSecond,0.5/gFramesPerSecond,0);
  281.     Q3Matrix4x4_Multiply(&m,&gTransformMatrix,&gTransformMatrix);    
  282.  
  283.  
  284.             /* UPDATE THE MATRIX */
  285.             
  286.     Q3MatrixTransform_Set(gTransformObject,&gTransformMatrix);
  287.  
  288.  
  289.             /* REDRAW IT */
  290.                 
  291.     DrawModelWindow();
  292. }
  293.  
  294.  
  295. /******************* DRAW MODEL WINDOW *********************/
  296. //
  297. // Calls the support function QD3D_DrawScene.
  298. // It passes the view info and a pointer to the game draw callback.
  299. //
  300.  
  301. void DrawModelWindow(void)
  302. {
  303.     QD3D_DrawScene(&gModelViewInfo,(void *)DrawTheGeometry);
  304. }
  305.  
  306.  
  307. /*************** DRAW THE GEOMETRY ******************/
  308.  
  309. void DrawTheGeometry(QD3DSetupOutputType *viewInfo)
  310. {
  311.     Q3Object_Submit(gBaseGroup,viewInfo->viewObject);
  312. }
  313.  
  314.  
  315. #pragma mark ======= COMPRESSION STUFF ========
  316.  
  317.  
  318. /****************** SELECT COMPRESSOR ************************/
  319.  
  320. void SelectCompressor(void)
  321. {
  322. ComponentInstance    aComponent;
  323. OSErr                iErr;
  324. PicHandle            picture;
  325. ComponentResult        aCompRes;
  326. Boolean                cancel = false;
  327. SCSpatialSettings    spatialInfo;
  328. TQ3Object            texture;
  329.  
  330.             /* CREATE COMPONENT */
  331.             
  332.     aComponent = OpenDefaultComponent('scdi', 'imag');
  333.     if (aComponent == nil)
  334.         DoFatalAlert("\pSelectCompressor: OpenDefaultComponent failed!");
  335.  
  336.             /* SET TEST PICT */
  337.             
  338.     picture = GetPicture(128);    
  339.     aCompRes = SCSetTestImagePictHandle(aComponent, picture, nil, 0);
  340.  
  341.  
  342.             /* DO THE DIALOG */
  343.                         
  344.     aCompRes = SCRequestImageSettings(aComponent);
  345.     if (aCompRes == scUserCancelled)
  346.     {
  347.         cancel = true;
  348.         goto cleanup;
  349.     }
  350.     if (aCompRes < 0)
  351.         DoFatalAlert("\pSelectCompressor: SCRequestSequenceSettings failed!");
  352.     
  353.     
  354.             /* EXTRACT INFO */
  355.     
  356.     aCompRes = SCGetInfo(aComponent, scSpatialSettingsType, &spatialInfo);        
  357.     if (aCompRes < 0)
  358.         DoFatalAlert("\pSelectCompressor: SCGetInfo failed!");
  359.  
  360.     gCodecType         = spatialInfo.codecType;                            // get codec type
  361.     gCodecComponent = spatialInfo.codec;                                // get codec component
  362.     gCodecQuality     = spatialInfo.spatialQuality;                        // get quality
  363.     gCodecDepth     = spatialInfo.depth;                                // get depth
  364.     
  365.     if (gCodecDepth & (1<<5))                                            // see if using greyscale
  366.         gCodecDepth2 = gCodecDepth & 0x1f;                                // mask out bit depth from encoded value
  367.     else
  368.         gCodecDepth2 = gCodecDepth;
  369.         
  370.  
  371.             /* CHANGE TEXTURE */
  372.             
  373.     Q3AttributeSet_Clear(gTextureAttr, kQ3AttributeTypeSurfaceShader);
  374.     texture = QD3D_GetTextureMap(128);                                    // get original texture (and knock to gCodecDepth)
  375.     Q3AttributeSet_Add(gTextureAttr, kQ3AttributeTypeSurfaceShader, &texture);        
  376.     Q3Object_Dispose(texture);
  377.  
  378.  
  379.             /* CLEANUP */
  380.             
  381. cleanup:            
  382.     ReleaseResource((Handle)picture);                                        // nuke pict
  383.     iErr = CloseComponent(aComponent);                                        // nuke component
  384.     if (iErr)
  385.         DoFatalAlert("\pSelectCompressor: CloseComponent failed!");
  386. }
  387.  
  388.  
  389.  
  390. /**************** QD3D GET TEXTURE MAP ***********************/
  391. //
  392. // Loads a PICT resource and returns a shader object which is
  393. // based on the PICT converted to a texture map.
  394. //
  395. // INPUT: textureRezID = resource ID of texture PICT to get.
  396. //            myFSSpec != nil if want to load PICT from file instead
  397. //
  398. // OUTPUT: TQ3ShaderObject = shader object for texture map.
  399. //
  400.  
  401. static TQ3SurfaceShaderObject    QD3D_GetTextureMap(long    textureRezID)
  402. {
  403. PicHandle                    picture;
  404. TQ3SurfaceShaderObject        shader;
  405.  
  406.             /* LOAD PICT REZ */
  407.         
  408.     picture = GetPicture (textureRezID);
  409.     if (picture == nil)
  410.         DoFatalAlert("\pUnable to load texture PICT resource");
  411.  
  412.  
  413.             /* CONVERT PICT TO TEXTURE OBJECT */
  414.             
  415.     shader = QD3D_PICTToTexture(picture);
  416.         
  417.     ReleaseResource ((Handle) picture);
  418.     return(shader);    
  419. }
  420.  
  421.  
  422. /**************** QD3D PICT TO TEXTURE ***********************/
  423. //
  424. //
  425. // INPUT: picture = handle to PICT.
  426. //
  427. // OUTPUT: TQ3ShaderObject = shader object for texture map.
  428. //
  429.  
  430. static TQ3SurfaceShaderObject    QD3D_PICTToTexture(PicHandle picture)
  431. {
  432. TQ3CompressedPixmap     pixmap;
  433. TQ3TextureObject        texture;
  434. TQ3SurfaceShaderObject    shader;
  435. long                    width, height,numBytesPerPixel;
  436. Rect                     rectGW;
  437. GWorldPtr                 pGWorld;
  438. PixMapHandle             hPixMap;
  439. OSErr                    myErr;
  440. GDHandle                oldGD;
  441. GWorldPtr                oldGW;
  442. unsigned long             pictMapAddr;
  443. unsigned long             pictRowBytes;
  444. TQ3Status                status;
  445.  
  446.                 /* CALC DIMENSIONS OF TEXTURE */
  447.                 
  448.     width = (**picture).picFrame.right  - (**picture).picFrame.left;
  449.     height = (**picture).picFrame.bottom - (**picture).picFrame.top;
  450.  
  451.  
  452.                 /* CREATE A GWORLD TO DRAW PICT INTO */
  453.  
  454.     GetGWorld(&oldGW, &oldGD);                                        // save current port
  455.     SetRect(&rectGW, 0, 0, width, height);                            // set dimensions
  456.     myErr = NewGWorld(&pGWorld, gCodecDepth2, &rectGW, 0, 0, 0L);    // make gworld
  457.     if (myErr)
  458.         DoFatalAlert("\pQD3D_PICTToTexture: NewGWorld failed!");
  459.  
  460.                 /* DRAW PICTURE INTO GWORLD */
  461.             
  462.     hPixMap = GetGWorldPixMap(pGWorld);                                // get gworld's pixmap
  463.     numBytesPerPixel = ((int)(**hPixMap).pixelSize/8);                // need this for ratio calculation below
  464.     SetGWorld(pGWorld, nil);    
  465.     LockPixels(hPixMap);
  466.     EraseRect(&rectGW);
  467.     DrawPicture(picture, &rectGW);
  468.  
  469.  
  470.             /****************************************/
  471.             /* MAKE A COMPRESSED PIXMAP FROM GWORLD */
  472.             /****************************************/
  473.             
  474.                 /* GET TEXTURE ADDR & ROWBYTES */
  475.                     
  476.     pictMapAddr = (unsigned long )GetPixBaseAddr(hPixMap);
  477.     pictRowBytes = (unsigned long)(**hPixMap).rowBytes & 0x3fff;
  478.  
  479.  
  480.             /* FILL OUT COMPRESSED PIXMAP STRUCTURE */
  481.  
  482.     pixmap.compressedImage = nil;
  483.     pixmap.imageDesc     = nil;
  484.     pixmap.makeMipmaps     = kQ3True;    
  485.     pixmap.width         = width;
  486.     pixmap.height        = height;
  487.     if (gCodecDepth == 32)
  488.     {
  489.         pixmap.pixelSize = 32;
  490.         pixmap.pixelType = kQ3PixelTypeRGB32;
  491.     }
  492.     else
  493.     {
  494.         pixmap.pixelSize = 16;
  495.         pixmap.pixelType = kQ3PixelTypeRGB16;
  496.     }
  497.  
  498.  
  499.  
  500.             /* COMPRESS IMAGE AND FILL OUT REMAINING RECORDS IN STRUCTURE */
  501.             //
  502.             // pass in compression quality, codec type, etc.
  503.             //
  504.             
  505.     status = Q3CompressedPixmapTexture_CompressImage(&pixmap, hPixMap, gCodecType, gCodecComponent,
  506.                                                     gCodecDepth, gCodecQuality);
  507.     SetGWorld (oldGW, oldGD);
  508.     DisposeGWorld (pGWorld);
  509.  
  510.     if (status == kQ3Failure)
  511.         DoFatalAlert("\pQ3CompressedPixmapTexture_CompressImage Failed!");    
  512.  
  513.     
  514.                 /* CALC COMPRESSION RATIO FOR DISPLAYING */
  515.                 
  516.     {
  517.         unsigned char *buffer;
  518.         unsigned long    compSize;
  519.         float            unCompSize;
  520.         
  521.         Q3MemoryStorage_GetBuffer(pixmap.compressedImage, &buffer, &compSize, &compSize);    // get compressed size            
  522.  
  523. //        unCompSize = width * height * numBytesPerPixel;                        // calc uncompressed size
  524.         unCompSize = width * height * (TEXTURE_ORIGINAL_DEPTH/8);            // calc uncompressed size (based on original depth)
  525.         
  526.         FloatToString((float)compSize/unCompSize,gCompressionRatio);        // calc compression ratio and convert to string
  527.  
  528.         NumToString(compSize, gCompressionSize);                            // also calc string for compressed size
  529.     }
  530.  
  531.  
  532.             /***************************/
  533.             /* MAKE NEW PIXMAP TEXTURE */
  534.             /***************************/
  535.             
  536.         /* MAKE NEW COMPRESSED PIXMAP TEXTUE OBJECT */
  537.                 
  538.     texture = Q3CompressedPixmapTexture_New (&pixmap);
  539.     if (texture == nil)
  540.         DoFatalAlert("\pError calling Q3CompressedPixmapTexture_New!");
  541.         
  542.         
  543.         /* MAKE NEW SHADER FROM TEXTURE */
  544.         
  545.     shader = Q3TextureShader_New (texture);
  546.     if (shader == nil)
  547.         DoFatalAlert("\pError calling Q3TextureShader_New!");
  548.  
  549.     Q3Object_Dispose(texture);
  550.     Q3Object_Dispose(pixmap.compressedImage);            // disposes of extra reference to storage objs
  551.     Q3Object_Dispose(pixmap.imageDesc);
  552.  
  553.     return(shader);    
  554. }
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567.  
  568.  
  569.